<?php
/**
 * 数据分发
 *
 * @copyright	http://www.easymvc.com
 * @author 		光哥 <439884988@qq.com>
 * @version		1.0 - 2012-07-02
 * @package		Lib
 */
if(!defined('EASYMVC')) {
	exit('Access Denied');
}

/**
 * get post cooke file 操作 
 */
class Lib_Request
{
    protected  $getdata;    //存储get的数据
    
     protected  $postdata;   //存储post的数据
    
     protected  $filedata;   //存储file数据
    
     protected  $cookiedata; //存储cookie
    
    
    
    function __construct()
    {
    	//控制层调整完后，将开启下边的注释，将$_GET,$_POST,$_FILES,$_COOKIE进行销毁
        $this->getdata    = self::formatData($_GET);
        //$_GET = array();
        $this->postdata   = self::formatData($_POST);
        //$_POST = array();
        $this->filedata   = self::formatData($_FILES);
        //$_FILES = array();
       	$this->cookiedata = self::formatData($_COOKIE);
        //$_COOKIE = array();
		
    }

	public static function getInstance(){
		static $obj;
		if(!$obj) {
			$obj = new self();
		}
		return $obj;
	}
    
	public function GetFile($key){
		return $this->filedata[$key];
	}
    
    /**
	 * 获取GET传递过来的数值变量
	 *
	 * @param string $key
	 * @return int or big int
	 */
	public	function GetNum($key)
	{
		return isset($this->getdata[$key]) ?  $this->toNum($this->getdata[$key]) :'';
	}
	
	/**
	 * 获取POST传递过来的数值变量
	 *
	 * @param string $key
	 * @return int or big int
	 */
	public	function PostNum($key)
	{
		return isset($this->postdata[$key]) ? $this->toNum($this->postdata[$key]) :'';
	}
	
	/**
	 * 获取GET传递过来的字符串变量
	 *
	 * @param string $key
	 * @param boolen $isfilter 是否过滤
	 * @return string
	 */
	public	function GetString($key,$isfilter=true)
	{
		if(isset($this->getdata[$key])) {
			if($isfilter){
				return $this->filterString($this->getdata[$key]);
			}else {
				return $this->getdata[$key];
			}
		}
		return false;
	}
	
	/**
	 * 获取POST传递过来的字符串变量
	 *
	 * @param string $key
	 * @param boolen $isfilter 是否过滤
	 * @return string
	 */
	public	function PostString($key,$isfilter=true)
	{
		if(isset($this->postdata[$key])) {
			if($isfilter )
			{
				return $this->filterString($this->postdata[$key]);
			}else {
				return $this->postdata[$key];
			}
		}
		return false;
	}
	
	
	/**
	 * 获取GET传递过来的字符串变量
	 *
	 * @param string $key
	 * @param boolen $isfilter 是否过滤
	 * @return string
	 */
	public	function GetCookieString($key,$isfilter=true)
	{
		if(isset($this->cookiedata[$key])) {
			if($isfilter){
				return $this->filterString($this->cookiedata[$key]);
			}else {
				return $this->cookiedata[$key];
			}
		}
		return false;
	}

	public function PostNumArray($key){
		if($this->postdata[$key]){
			return array_map('intval',$this->postdata[$key]);
		}
	}

	public function GetNumArray($key){
		if($this->getdata[$key]) {
			return array_map('intval',$this->getdata[$key]);
		}
	}
	
	/**
	 * 格式化数据,将数据转存
	 *
	 * @param array $data
	 */
	private	function formatData($data)
	{
		if(is_array($data)) return $data;
		$result = array();
        reset($data);
        while(list($key, $value) = each($data))
        {
            $result[$key] = trim($value);
        }
        print_r($data);
        return $result;
	}
	/**
	 * 转换为数字
	 *
	 * @param string $num
	 * @return int or big int or false
	 */
	private function toNum($num)
	{
		if(is_numeric($num))
		{
			return intval($num);
		}else{
			return false;
		}
	}
	/**
	 * 转换过滤字符串
	 *
	 * @param string $string
	 * @return string
	 */
	private function filterString($string)
	{
		if($string===NULL)
		{
			return false;
		}
		return htmlspecialchars($string);
	}


}
?>